#Boa:Frame:cadProdFrame

import wx
import wx.grid
from CONTROLE import *

def create(parent):
    return cadProdFrame(parent)

[wxID_CADPRODFRAME, wxID_CADPRODFRAMEALTERARBTN, wxID_CADPRODFRAMECANCELARBTN, 
 wxID_CADPRODFRAMEEXCLUIRBTN, wxID_CADPRODFRAMELIMPARBTN, 
 wxID_CADPRODFRAMELISTBOX1, wxID_CADPRODFRAMENOVOBTN, 
 wxID_CADPRODFRAMEPESQUISABTN, wxID_CADPRODFRAMESALVARBTN, 
 wxID_CADPRODFRAMESTATICBOX1, wxID_CADPRODFRAMESTATICBOX2, 
 wxID_CADPRODFRAMESTATICBOX3, wxID_CADPRODFRAMESTATICTEXT1, 
 wxID_CADPRODFRAMESTATICTEXT2, wxID_CADPRODFRAMESTATICTEXT3, 
 wxID_CADPRODFRAMESTATICTEXT4, wxID_CADPRODFRAMESTATICTEXT5, 
 wxID_CADPRODFRAMESTATICTEXT6, wxID_CADPRODFRAMETEXTCTRL1, 
 wxID_CADPRODFRAMETEXTCTRL2, wxID_CADPRODFRAMETEXTCTRL3, 
 wxID_CADPRODFRAMETEXTCTRL4, wxID_CADPRODFRAMETEXTCTRL5, 
 wxID_CADPRODFRAMETXTPESQUISA, 
] = [wx.NewId() for _init_ctrls in range(24)]

class cadProdFrame(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_CADPRODFRAME, name=u'cadProdFrame',
              parent=prnt, pos=wx.Point(394, 167), size=wx.Size(566, 489),
              style=wx.DEFAULT_FRAME_STYLE, title=u'Cadastro de Produtos')
        self.SetClientSize(wx.Size(550, 451))

        self.staticBox1 = wx.StaticBox(id=wxID_CADPRODFRAMESTATICBOX1,
              label=u'Controle', name=u'staticBox1', parent=self,
              pos=wx.Point(408, 48), size=wx.Size(128, 224), style=0)

        self.staticBox2 = wx.StaticBox(id=wxID_CADPRODFRAMESTATICBOX2,
              label=u'Pesquisa', name='staticBox2', parent=self, pos=wx.Point(8,
              280), size=wx.Size(528, 164), style=0)

        self.staticBox3 = wx.StaticBox(id=wxID_CADPRODFRAMESTATICBOX3,
              label=u'Cadastro de Produtos', name='staticBox3', parent=self,
              pos=wx.Point(8, 48), size=wx.Size(384, 224), style=0)

        self.novoBtn = wx.Button(id=wxID_CADPRODFRAMENOVOBTN, label=u'Novo',
              name=u'novoBtn', parent=self, pos=wx.Point(432, 80),
              size=wx.Size(75, 23), style=0)
        self.novoBtn.Bind(wx.EVT_BUTTON, self.OnNovoBtnButton,
              id=wxID_CADPRODFRAMENOVOBTN)

        self.alterarBtn = wx.Button(id=wxID_CADPRODFRAMEALTERARBTN,
              label=u'Alterar', name=u'alterarBtn', parent=self,
              pos=wx.Point(432, 112), size=wx.Size(75, 23), style=0)
        self.alterarBtn.Bind(wx.EVT_BUTTON, self.OnAlterarBtnButton,
              id=wxID_CADPRODFRAMEALTERARBTN)

        self.salvarBtn = wx.Button(id=wxID_CADPRODFRAMESALVARBTN,
              label=u'Salvar', name=u'salvarBtn', parent=self, pos=wx.Point(432,
              144), size=wx.Size(75, 23), style=0)
        self.salvarBtn.Bind(wx.EVT_BUTTON, self.OnSalvarBtnButton,
              id=wxID_CADPRODFRAMESALVARBTN)

        self.cancelarBtn = wx.Button(id=wxID_CADPRODFRAMECANCELARBTN,
              label=u'Cancelar', name=u'cancelarBtn', parent=self,
              pos=wx.Point(432, 176), size=wx.Size(75, 24), style=0)
        self.cancelarBtn.Bind(wx.EVT_BUTTON, self.OnCancelarBtnButton,
              id=wxID_CADPRODFRAMECANCELARBTN)

        self.excluirBtn = wx.Button(id=wxID_CADPRODFRAMEEXCLUIRBTN,
              label=u'Excluir', name=u'excluirBtn', parent=self,
              pos=wx.Point(432, 208), size=wx.Size(75, 23), style=0)
        self.excluirBtn.Bind(wx.EVT_BUTTON, self.OnExcluirBtnButton,
              id=wxID_CADPRODFRAMEEXCLUIRBTN)

        self.staticText1 = wx.StaticText(id=wxID_CADPRODFRAMESTATICTEXT1,
              label=u'Cadastro de Produtos', name='staticText1', parent=self,
              pos=wx.Point(160, 8), size=wx.Size(201, 25), style=0)
        self.staticText1.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u'Tahoma'))

        self.pesquisaBtn = wx.Button(id=wxID_CADPRODFRAMEPESQUISABTN,
              label=u'Pesquisa', name=u'pesquisaBtn', parent=self,
              pos=wx.Point(416, 296), size=wx.Size(56, 23), style=0)
        self.pesquisaBtn.Bind(wx.EVT_BUTTON, self.OnPesquisaBtnButton,
              id=wxID_CADPRODFRAMEPESQUISABTN)

        self.limparBtn = wx.Button(id=wxID_CADPRODFRAMELIMPARBTN,
              label=u'Limpar', name=u'limparBtn', parent=self, pos=wx.Point(480,
              296), size=wx.Size(40, 23), style=0)
        self.limparBtn.Bind(wx.EVT_BUTTON, self.OnLimparBtnButton,
              id=wxID_CADPRODFRAMELIMPARBTN)

        self.txtPesquisa = wx.TextCtrl(id=wxID_CADPRODFRAMETXTPESQUISA,
              name=u'txtPesquisa', parent=self, pos=wx.Point(24, 296),
              size=wx.Size(384, 21), style=0, value=u'')

        self.staticText2 = wx.StaticText(id=wxID_CADPRODFRAMESTATICTEXT2,
              label=u'Codigo', name='staticText2', parent=self, pos=wx.Point(32,
              80), size=wx.Size(34, 13), style=0)

        self.staticText3 = wx.StaticText(id=wxID_CADPRODFRAMESTATICTEXT3,
              label=u'Nome', name='staticText3', parent=self, pos=wx.Point(32,
              112), size=wx.Size(28, 13), style=0)

        self.staticText4 = wx.StaticText(id=wxID_CADPRODFRAMESTATICTEXT4,
              label=u'Quantidade', name='staticText4', parent=self,
              pos=wx.Point(32, 144), size=wx.Size(57, 13), style=0)

        self.staticText5 = wx.StaticText(id=wxID_CADPRODFRAMESTATICTEXT5,
              label=u'Pre\xe7o de compra', name='staticText5', parent=self,
              pos=wx.Point(32, 176), size=wx.Size(81, 13), style=0)

        self.staticText6 = wx.StaticText(id=wxID_CADPRODFRAMESTATICTEXT6,
              label=u'Pre\xe7o de venda', name='staticText6', parent=self,
              pos=wx.Point(32, 208), size=wx.Size(76, 13), style=0)

        self.textCtrl1 = wx.TextCtrl(id=wxID_CADPRODFRAMETEXTCTRL1,
              name='textCtrl1', parent=self, pos=wx.Point(120, 72),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl1.SetEditable(False)
        self.textCtrl1.Enable(False)

        self.textCtrl2 = wx.TextCtrl(id=wxID_CADPRODFRAMETEXTCTRL2,
              name='textCtrl2', parent=self, pos=wx.Point(120, 104),
              size=wx.Size(256, 21), style=0, value=u'')
        self.textCtrl2.SetEditable(False)
        self.textCtrl2.Enable(False)

        self.textCtrl3 = wx.TextCtrl(id=wxID_CADPRODFRAMETEXTCTRL3,
              name='textCtrl3', parent=self, pos=wx.Point(120, 136),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl3.SetEditable(False)
        self.textCtrl3.Enable(False)

        self.textCtrl4 = wx.TextCtrl(id=wxID_CADPRODFRAMETEXTCTRL4,
              name='textCtrl4', parent=self, pos=wx.Point(120, 168),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl4.SetEditable(False)
        self.textCtrl4.Enable(False)

        self.textCtrl5 = wx.TextCtrl(id=wxID_CADPRODFRAMETEXTCTRL5,
              name='textCtrl5', parent=self, pos=wx.Point(120, 200),
              size=wx.Size(100, 21), style=0, value=u'')
        self.textCtrl5.SetEditable(False)
        self.textCtrl5.Enable(False)

        self.listBox1 = wx.ListBox(choices=[], id=wxID_CADPRODFRAMELISTBOX1,
              name='listBox1', parent=self, pos=wx.Point(24, 344),
              size=wx.Size(496, 88), style=0)
        self.listBox1.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBox1ListboxDclick,
              id=wxID_CADPRODFRAMELISTBOX1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnNovoBtnButton(self, event):
        self.alterar_campos(True)
        self.textCtrl1.SetValue('')
        self.textCtrl2.SetValue('')
        self.textCtrl3.SetValue('')
        self.textCtrl4.SetValue('')
        self.textCtrl5.SetValue('')
        
    def alterar_campos(self, VF):


        self.textCtrl2.SetEditable(VF)
        self.textCtrl2.Enable(VF)

        self.textCtrl5.SetEditable(VF)
        self.textCtrl5.Enable(VF)

    def OnAlterarBtnButton(self, event):
        self.alterar_campos(True)

    def OnSalvarBtnButton(self, event):
        c = Controle()
        
        cod = self.textCtrl1.GetValue()
        nome = self.textCtrl2.GetValue()
        qtd = self.textCtrl3.GetValue()
        preco_compra = self.textCtrl4.GetValue()
        preco_venda = self.textCtrl5.GetValue()
        
        atributos = [cod, nome, qtd, preco_compra, preco_venda]

        validacao = c.salvar_produto(atributos)
        
        if validacao == True:
            m = 'Produto Salvo com Sucesso'
            c = 'Cadastro de Produtos'
            wx.MessageBox(m,c,wx.OK)
            
        else:
            m = 'ERRO!'
            c = 'Cadastro de Produtos'
            wx.MessageBox(m,c,wx.OK)
            
        self.alterar_campos(False)
            
         
        

    def OnCancelarBtnButton(self, event):
        self.Close()

    def OnExcluirBtnButton(self, event):
        c = Controle()
        cod = self.textCtrl1.GetValue()
        validacao = c.remover_produto(cod)
        if validacao == True:
            m = 'Produto removido com Sucesso'
            c = 'Cadastro de Produtos'
            wx.MessageBox(m,c,wx.OK)
            
        else:
            m = 'ERRO!'
            c = 'Cadastro de Produtos'
            wx.MessageBox(m,c,wx.OK)

    def OnPesquisaBtnButton(self, event):
        self.listBox1.Clear()
        nome = self.txtPesquisa.GetValue()
        c = Controle()
        lista = c.procurar_produto_nome(nome)
        
        def codd(x):
            x = str(x)
            while len(x)<15:
                x = x+' '
            return x
            
        def desc(x):
            x = str(x)
            while len(x)<60:
                x = x+' '
            return x
            
        def quant(x):
            x = str(x)
            while len(x)<15:
                x = x+' '
            return x
            
        def pcompra(x):
            x = str(x)
            while len(x)<15:
                x = x+' '
            return x
        
        def pvenda(x):
            x = str(x)
            while len(x)<15:
                x = x+' '
            return x
            

        for r in lista:
            addd = codd(+r[0])+desc(r[1])+quant(r[2])+pcompra(r[3])+pvenda(r[4])
            self.listBox1.Append(addd)
                   
    def OnLimparBtnButton(self, event):
        self.txtPesquisa.SetValue('')
        self.listBox1.Clear()

    def OnListBox1ListboxDclick(self, event):
        a = self.listBox1.GetStringSelection()
        cod = a[0:3]
        cod = cod.split(" ")
        cod = str(cod[0])
        
        c = Controle()
        atributos = c.procurar_produto_codigo(cod)
        
        self.textCtrl1.SetValue(str(atributos[0]))
        self.textCtrl2.SetValue(str(atributos[1]))
        self.textCtrl3.SetValue(str(atributos[2]))
        self.textCtrl4.SetValue(str(atributos[3]))
        self.textCtrl5.SetValue(str(atributos[4]))
        
        self.alterar_campos(False)
